package com.minelx.woods.data.values;

import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Objects;

import static java.util.stream.Collectors.toList;

public final class InfoValues {
	private final String sourceType;

	private final JSONObject sourceVariables;

	private final List<String> collections;

	private final List<VariantValues> variantCollections;

	public InfoValues(String sourceType, JSONObject sourceVariables, List<String> collections, List<VariantValues> variantCollections) {
		this.sourceType = sourceType;
		this.sourceVariables = sourceVariables;
		this.collections = collections;
		this.variantCollections = variantCollections;
	}

	public String getSourceType() {
		return sourceType;
	}

	public JSONObject getSourceVariables() {
		return sourceVariables;
	}

	public List<String> getCollections() {
		return collections;
	}

	public List<VariantValues> getVariantCollections() {
		return variantCollections;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		InfoValues that = (InfoValues) o;
		return sourceType.equals(that.sourceType) && sourceVariables.equals(that.sourceVariables) && collections.equals(that.collections) && variantCollections.equals(that.variantCollections);
	}

	@Override
	public int hashCode() {
		return Objects.hash(sourceType, sourceVariables, collections, variantCollections);
	}

	@Override
	public String toString() {
		return "InfoValues{" +
				"sourceType='" + sourceType + '\'' +
				", sourceVariables=" + sourceVariables +
				", collections=" + collections +
				", variantCollections=" + variantCollections +
				'}';
	}

	public static InfoValues from(JSONObject info) {
		return new InfoValues(
				info.getString("sourceType"),
				info.getJSONObject("sourceVariables"),
				info.getJSONArray("collections").toJavaList(String.class),
				info.getJSONArray("variantCollections").toJavaList(JSONObject.class).stream()
						.map(VariantValues::fromJSON)
						.collect(toList())
		);
	}
}
